/**
 * Created by Fly on 2016/9/12.
 */

$(function () {
    $('#addart_form').submit(function () {
        var formdata=new FormData($('#addart_form')[0]);
        $.ajax({
            type: 'POST',
            url: '/addart',
            data:formdata,
            async:false,
            cache:false,
            contentType:false,
            processData:false,
            success: function (data) {
                if(data.res==1){
                    location.href='/article';
                }else if(data.res==0){
                    location.href='/error';
                }else{
                    alert(data);
                }
            },
            error: function (data) {
                alert(data);
            }
        });
        return false;
    });

    $("#exampleInputFile").change(function(){
        var objUrl = getObjectURL(this.files[0]);
        console.log("objUrl = "+objUrl) ;
        if (objUrl) {
            $("#preview_img").attr("src", objUrl) ;
        }
    }) ;
    function getObjectURL(file) {
        var url = null ;
        if (window.createObjectURL!=undefined) { // basic
            url = window.createObjectURL(file) ;
        } else if (window.URL!=undefined) { // mozilla(firefox)
            url = window.URL.createObjectURL(file) ;
        } else if (window.webkitURL!=undefined) { // webkit or chrome
            url = window.webkitURL.createObjectURL(file) ;
        }
        return url ;
    }

});